var postData = require("../../../data/posts-data.js");
var app = getApp();
Page({
    data: {
        currentPostId: ""
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(app.globalData.g_PlayingMusic);
        this.setData({
            obj: postData.data[options.id],//获取详细的新闻信息
            currentPostId: options.id//获取新闻ID
        })

        if (app.globalData.g_musicId===options.id){
            this.setData({ isPlaying: app.globalData.g_PlayingMusic })//获取音乐播放状态
        }
        // wx.setStorageSync
        // wx.getStorageSync
        // wx.remoteStorage
        // wx.clearStorage
        //小程序规定缓存(本地存储)最多存储10MB
        var coll = wx.getStorageSync("post_collected");
        if (coll) {
            var state = coll[options.id];
            this.setData({
                collected: state
            })
        } else {
            coll = {};
            coll[options.id] = false;
            wx.setStorageSync("post_collected", coll);
        }

        //让微信的音乐总控开关与页面保持一致
        // wx.onBackgroundAudioPlay(() => app.setGlobalData({ g_PlayingMusic: true }));
        wx.onBackgroundAudioPlay(() => { this.setData({ isPlaying: true }); app.globalData.g_PlayingMusic = true; });
        wx.onBackgroundAudioPause(() => { this.setData({ isPlaying: false }); app.globalData.g_PlayingMusic = false });
        wx.onBackgroundAudioStop(() => { this.setData({ isPlaying: false }); app.globalData.g_PlayingMusic = false });
    },
    onSwitchTap: function (event) {
        this.onSwitchTapAsy();//异步
        //   this.onSwitchTapSync();//同步
    },
    onSwitchTapAsy: function (event) {
        var that = this;
        wx.getStorage({
            key: 'post_collected',
            success: function (res) {
                var coll = res.data;
                var state = coll[that.data.currentPostId];
                state = !state;
                //可以通过this.data取数据，不能存。存必须用setData
                coll[that.data.currentPostId] = state;
                that.onShowModal(state, coll);
            },
        })
    },
    onSwitchTapSync: function (event) {
        var coll = wx.getStorageSync("post_collected");
        var state = coll[this.data.currentPostId];
        state = !state;
        //可以通过this.data取数据，不能存。存必须用setData
        coll[this.data.currentPostId] = state;
        this.onShowModal(state, coll);
    },
    onShowToast: function (state, coll) {
        wx.setStorageSync("post_collected", coll);
        this.setData({
            collected: state
        })
        //提示框
        wx.showToast({
            title: state ? "收藏成功" : "取消成功",
            duration: 1000
        })
    },
    onShowModal: function (state, coll) {
        //模式框
        wx.showModal({
            title: '收藏',
            content: state ? '是否收藏该文章？' : "是否取消收藏该文章？",
            showCancel: true,
            cancelText: "取消",
            cancelColor: "#333",
            confirmText: "确定",
            confirmColor: "#405f80",
            success: (res) => {//箭头函数
                //获取用户选择：如果选择了确定
                if (res.confirm) {
                    wx.setStorageSync("post_collected", coll);
                    this.setData({
                        collected: state
                    })
                }
            }
        })
    },
    onShareTap: function (event) {
        var arr = ["分享给微信好友", "分享到朋友圈", "分享到QQ", "分享到微博"];
        wx.showActionSheet({
            itemList: arr,
            itemColor: "#405f80",
            success: function (res) {
                if (res.cancel) {
                    wx.showToast({
                        title: '选择了取消',
                    })
                } else {
                    wx.showToast({
                        title: '选择了' + arr[res.tapIndex],
                    })
                }
            }
        })
    },
    onMusicTap: function () {
        //设置全局音乐播放页ID
        app.globalData.g_musicId = this.data.currentPostId;
        //根据当前新闻ID，查找详情
        var item = postData.data[this.data.currentPostId];
        var isPlaying = this.data.isPlaying;
        console.log("播放状态："+isPlaying);
        //如果当前播放状态是true，则停止播放
        if (isPlaying) {
            console.log("暂停");
            wx.pauseBackgroundAudio();
            isPlaying = false;
        } else {
            isPlaying = true;
            wx.playBackgroundAudio({
                dataUrl: item.music.dataUrl,
                title: item.music.title,
                coverImgUrl: item.music.coverImgUrl
            })
        }
        this.setData({ isPlaying: isPlaying });
    }
})